﻿using System;
using System.Linq;

namespace Fee.ViewModel
{
    public class StationViewModel
    {
        public StationViewModel()
        {
        }

        public StationViewModel(Station station)
        {
            if (station == null) throw new ArgumentNullException(nameof(station));
            Id = station.Id;
            District = new DistrictSimpleViewModel(station.District);
            Code = station.Code;
            Type = station.Type;
            IsVip = station.IsVip;
            Name = station.Name;
            BuildBefore2017 = Station.IsBefore2017(station.CreateTime);
            Status = station.Status;
            ThirdStationStatus = station.ThirdStationStatus;
            UsersNumber = station.UsersNumber;
          
            OrderDistrict = new DistrictSimpleViewModel(station.OrderDistrict);
        }
        public string Name { get; set; }
        public int UsersNumber { get; set; }
        public bool IsVip { get; set; }
        public StationStatus? Status { get; set; }
        public bool BuildBefore2017 { get; set; }
        public int Id { get; set; }

        public string ThirdStationStatus { get; set; }
        public DistrictSimpleViewModel District { get; set; }

        public DistrictSimpleViewModel OrderDistrict { get; set; }

        public string Code { get; set; }
        public StationType? Type { get; set; }

        public Station ToStation(FeeDbContext db)
        {
            return new Station
            {
                OrderDistrict = OrderDistrict != null ? db.Districts.FirstOrDefault(_ => _.Id == OrderDistrict.Id) : null,
                District = District != null ? db.Districts.FirstOrDefault(_ => _.Id == District.Id) : null,
                Code = Code,

                Type = Type ?? StationType.CustomerGroupCoverage,
                ThirdStationStatus = ThirdStationStatus,
                CreateTime = BuildBefore2017 ? Station.Date2017 : DateTimeOffset.Now,
                Id = Id,
                Name = Name,
                UsersNumber= UsersNumber,
                IsVip = IsVip,
            };
        }
    }
}